package com.cube.backribs.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

public class BitmapUtils {

    public Bitmap bitmapFromFile(String fileDataStream) {
        File file = new File(fileDataStream);
        BitmapFactory.Options options = new BitmapFactory.Options();

        if (file.length() > 8000000) {
            options.inSampleSize = 16;
        } else if (file.length() > 4000000) {
            options.inSampleSize = 8;
        } else if (file.length() > 2000000) {
            options.inSampleSize = 4;
        } else {
            options.inSampleSize = 2;
        }

        Bitmap bitmap = BitmapFactory.decodeFile(fileDataStream, options);
        return bitmap;
    }

    public Bitmap bitmapFromFile(String fileDataStream, int type, int rotation) {
        // decode image size
        BitmapFactory.Options boundOptions = new BitmapFactory.Options();
        boundOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(fileDataStream, boundOptions);

        // Find the correct scale value. It should be the power of 2.
        int width = boundOptions.outWidth;
        int height = boundOptions.outHeight;
        int scale = 1;

        while (true) {
            if (width / 2 < CommonValues.RESOLUTION[type][CommonValues.WIDTH]
                    || height / 2 < CommonValues.RESOLUTION[type][CommonValues.HEIGHT]) {
                break;
            }

            width /= 2;
            height /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeFile(fileDataStream, options);

        if (rotation != 0) {
            return rotateBitmap(bitmap, rotation);
        } else {
            return bitmap;
        }
    }

    public Bitmap bitmapFromFile(File file, int type) {
        try {
            // decode image size
            BitmapFactory.Options boundOptions = new BitmapFactory.Options();
            boundOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(file), null,
                    boundOptions);

            // Find the correct scale value. It should be the power of 2.
            int width = boundOptions.outWidth;
            int height = boundOptions.outHeight;
            int scale = 1;

            while (true) {
                if (width / 2 < CommonValues.RESOLUTION[type][CommonValues.WIDTH]
                        || height / 2 < CommonValues.RESOLUTION[type][CommonValues.HEIGHT]) {
                    break;
                }

                width /= 2;
                height /= 2;
                scale *= 2;
            }

            // decode with inSampleSize
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = scale;
            Bitmap bitmap = BitmapFactory.decodeStream(
                    new FileInputStream(file), null, options);
            return bitmap;
        } catch (FileNotFoundException e) {

        }

        return null;
    }

    public int getOrientationRotate(String path) {
        int rotation = 0;
        int rotationInDegrees = 0;

        try {
            ExifInterface exif = new ExifInterface(path);
            rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            rotationInDegrees = exifToDegrees(rotation);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return rotationInDegrees;
    }

    private Bitmap rotateBitmap(Bitmap source, int degrees) {
        Matrix matrix = new Matrix();
        matrix.preRotate(degrees);

        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
    }

    private int exifToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }

        return 0;
    }

}
